﻿Public Class clsInhStop
    Inherits clsStop

#Region "Attributes"
    Dim mvarStop As New DataTable()
    Dim mvarSklad As New Collection(Of clsInhSklad)
    Dim mvarSkladDel As New Collection(Of clsInhSklad)
#End Region

#Region "Properties"

    Public Property NazwaStop() As DataTable
        Get
            Return mvarStop
        End Get
        Set(ByVal value As DataTable)
            mvarStop = value
        End Set
    End Property

    Public Property Sklad() As Collection(Of clsInhSklad)
        Get
            Return mvarSklad
        End Get
        Set(ByVal value As Collection(Of clsInhSklad))
            mvarSklad = value
        End Set
    End Property

    Public Property SkladDel() As Collection(Of clsInhSklad)
        Get
            Return mvarSkladDel
        End Get
        Set(ByVal value As Collection(Of clsInhSklad))
            mvarSkladDel = value
        End Set
    End Property

#End Region

#Region "Methods"

    Public Sub New()
        UtworzTabele()
        mvarSklad = New Collection(Of clsInhSklad)
        mvarSkladDel = New Collection(Of clsInhSklad)
    End Sub

    Private Sub UtworzTabele()

        With mvarStop.Columns
            .Add("stop", GetType(clsInhStop))
        End With

    End Sub

    Public Sub New(ByVal ID As Integer, ByVal lvarDbHolder As clsDataBase)

        MyBase.New(ID.ToString(), lvarDbHolder, VAR_ERROR_HANDLER)

        mvarSklad = New Collection(Of clsInhSklad)
        mvarSkladDel = New Collection(Of clsInhSklad)

        Dim lvarTabel As DataTable = SzukajSkladu(lvarDbHolder, ID)

        If Not lvarTabel Is Nothing Then
            For Each lvarRow As DataRow In lvarTabel.Rows
                mvarSklad.Add(New clsInhSklad(CInt(lvarRow.Item("id")), lvarDbHolder))
            Next
        End If

    End Sub

    Public Overloads Sub Save(ByVal lvarDbHolder As clsDataBase)

        lvarDbHolder.BeginTrans()
        MyBase.Save(lvarDbHolder, VAR_ERROR_HANDLER)

        For Each lvarPozycja As clsInhSklad In mvarSkladDel
            Dim lvarTabel As DataTable = SzukajWSkladzieChemicznym(lvarDbHolder, lvarPozycja.Id)
            If Not lvarTabel Is Nothing Then
                For Each lvarRow As DataRow In lvarTabel.Rows
                    Dim lvarSkladChemiczny As New clsInhSkladChemiczny(CInt(lvarRow.Item("id")), lvarDbHolder)
                    lvarSkladChemiczny.Delete(lvarDbHolder)
                Next
            End If
            lvarPozycja.Delete(lvarDbHolder)
        Next

        For Each lvarPozycja As clsInhSklad In mvarSklad
            lvarPozycja.StopId = CInt(MyBase.Id)
            lvarPozycja.Save(lvarDbHolder)
        Next
        lvarDbHolder.CommitTrans()

    End Sub

    Public Sub Szukaj(ByVal lvarDbHolder As clsDataBase, ByVal Zapytanie As String)
        Dim lvarNewRow As DataRow
        Dim lvarReader As OdbcDataReader = Nothing
        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_oem", "cls_stop", 1, VAR_ERROR_HANDLER)
            With lvarSqlRunner
                .AddParameter("p_zapytanie", Zapytanie)
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                While lvarReader.Read
                    lvarNewRow = mvarStop.NewRow()
                    With lvarNewRow
                        .Item("stop") = New clsInhStop(CInt(IIf(IsDBNull(lvarReader.Item("id")), -1, lvarReader.Item("id"))), lvarDbHolder)
                    End With
                    mvarStop.Rows.Add(lvarNewRow)
                End While
            End If
        End If

    End Sub

    Shared Function SzukajSkladu(ByVal lvarDbHolder As clsDataBase, Id As Integer) As DataTable

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As DataTable = Nothing

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_oem", "cls_stop", 2, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_zapytanie", Id.ToString())
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = New DataTable
                lvarReturn.Load(lvarReader)
            End If

        End If

        Return lvarReturn

    End Function

    Shared Function SzukajWSkladzieChemicznym(ByVal lvarDbHolder As clsDataBase, Id As Integer) As DataTable

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As DataTable = Nothing

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_oem", "cls_stop", 3, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_id", Id.ToString())
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = New DataTable
                lvarReturn.Load(lvarReader)
            End If

        End If

        Return lvarReturn

    End Function

#End Region

End Class
